# -*- coding: utf-8 -*-
'''
Created on Oct 8, 2013

@author: LONG HOANG GIANG
'''
from urlparse import urljoin
import logging
import os
import sys
import traceback
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import lib
import json
import workerpool

logging.basicConfig(level=logging.DEBUG, format='%(levelname)s :: %(asctime)s :: %(message)s', datefmt='%d/%m/%Y %H:%M:%S')

class MyJob(workerpool.Job):
    
    def __init__(self, func, chapter):
        self.func = func
        self.chapter = chapter
        
    def run(self):
        self.func(self.chapter)
        
        
class TruyenNgonTinh():
    
    catFolder = ""
    
    def getDetail(self, chapter):
        try:
            print '> get detail of {0} with url {1}'.format(chapter['name'], chapter['url'])
            tree = lib.Web.load(chapter['url']).build_tree()
            xpathList = ["//div[@class='entry']", "//div[@class='entry-content']//div[@style='display: none']/preceding-sibling::*[1]"]
            for iXpath in xpathList:
                contentNode = tree.xpath(iXpath)
                if len(contentNode) == 0: continue
                lib.Etree.cleanNode("./div[@class='ann']", contentNode)
                lib.Etree.cleanNodeNextSibling(contentNode[0].xpath(".//div[@style='display: none']"), True)
                content = lib.html2text(contentNode[0], chapter['url'])
                print content
                content = '''<h4>{0}</h4><div id="content">{1}</div>'''.format(chapter['name'], content)
                path = self.storyPath + "/files/" + chapter['path']
                lib.gz_file_put_content(json.dumps(content), path)
        except:
            logging.error(chapter['name'] + "- " + chapter['url'])
            traceback.print_exc()
    
    def getChapters(self, url):
        logging.info("getChapters - url: {0}".format(url))
        data = [{'name': 'GIỚI THIỆU', 'url': url, 'path': 'gioi-thieu'}]
        tree = lib.Web.load(url).build_tree()
        nodes = tree.xpath("//div/h3[contains(., 'Đọc từ đầu')]/following-sibling::*[1]/li/a".decode('utf-8'))
        if len(nodes) == 0:
            lib.Web.removeCache(url)
            tree = lib.Web.load(url).build_tree()
        for node in nodes:
            name = lib.stringify(node)
            link = node.get('href')
            if name == '' or link == '': continue
            name = lib.toUpper(name)
            link = urljoin(url, link)
            data.append({'name': name, 'url': link, 'path': lib.crc32unsigned(link)})
            
        for i in data:
            print i['name'], i['url']
        
        return data
    
    def processStory(self, url):
        path = lib.extractText(r"/truyen/(.+)/", url, 1).strip()
        self.storyPath = "/longhoanggiang/truyentext/{0}".format(path) if self.catFolder == '' else "/longhoanggiang/truyentext/{0}".format(self.catFolder).format(path)
        print self.storyPath
        lib.makedir(self.storyPath + "/files")
        chapters = self.getChapters(url)
        for c in chapters:
            print c['name']
        pool = workerpool.WorkerPool(size=5)
        for chapter in chapters:
            pool.put(MyJob(self.getDetail, chapter))
        pool.shutdown()
        pool.wait()
        for c in chapters:
            del c['url']
        if len(chapters) > 0:
            lib.gz_file_put_content(lib.encryptCipher(json.dumps(chapters)), self.storyPath + "/data")
            
            
if __name__ == '__main__':
    
    
#     t = TruyenNgonTinh()
#     chapters = t.getChapters('http://truyenngontinh.com/truyen/cuoi-con-duong-gap-tinh-yeu/')
#     t.getDetail({'name': '', 'url': 'http://truyenngontinh.com/cuc-cung-kieu-ngao-pk-tong-tai-papa-van-an/', 'path': ''})
    url = raw_input("Enter URL: ")
    if url.startswith("http"):
        t = TruyenNgonTinh()
        t.processStory(url)
    
    logging.info("finished")
    os._exit(1)
            